package com.p2p.front.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.p2p.base.model.Money;
import com.p2p.front.dal.dao.ChargeLogSummaryDao;
import com.p2p.front.dal.model.ChargeLog;
import com.p2p.front.dal.model.ChargeLogSummary;
import com.p2p.front.enums.ChargeType;
import com.p2p.front.service.ChargeLogSummaryService;

/**
 * leiwen@yiqihao.com
 */
@Service
public class ChargeLogSummaryServiceImpl implements ChargeLogSummaryService {

	@Resource
	private ChargeLogSummaryDao chargeLogSummaryDao;

	public void add(ChargeLogSummary chargeLogSummary) {
		chargeLogSummaryDao.add(chargeLogSummary);
	}

	@Override
	public void update(ChargeLog chargeLog) {
		ChargeLogSummary chargeLogSummary = chargeLogSummaryDao
				.selectForUpdate(chargeLog.getUid(),chargeLog.getType());
		if (chargeLogSummary == null) {
			chargeLogSummary = new ChargeLogSummary();
			chargeLogSummary.setUid(chargeLog.getUid());
			chargeLogSummary.setType(chargeLog.getType());
			chargeLogSummary.setAmount(new Money(0L));
			chargeLogSummaryDao.add(chargeLogSummary);
			chargeLogSummary = chargeLogSummaryDao.selectForUpdate(chargeLog
					.getUid(),chargeLog.getType());
		}
		chargeLogSummary.setUid(chargeLog.getUid());
		chargeLogSummary.setType(chargeLog.getType());
		Money newAmount = new Money(chargeLogSummary.getAmount().getCent()
				+ chargeLog.getTransAmount().getCent());
		chargeLogSummary.setAmount(newAmount);
		chargeLogSummaryDao.update(chargeLogSummary);
	}
	
	@Override
	public ChargeLogSummary get(Long uid,ChargeType type){
		return chargeLogSummaryDao.get(uid,type.name());
		
	}
}
